package com.hyj.dev.test;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;

import javax.imageio.ImageIO;

import net.sourceforge.tess4j.ITesseract;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;

/**
 * Hello world!
 *
 */
public class App {
	public static void main(String[] args) throws URISyntaxException {
		// 识别图片的路径（修改为自己的图片路径）
		String path = "C:\\Users\\hyj\\Desktop\\图片\\yzm.png";
		
		String path1 = App.class.getProtectionDomain().getCodeSource().getLocation().getPath();
		 String path2 = App.class.getProtectionDomain().getCodeSource().getLocation().getFile();
		 URL location = App.class.getProtectionDomain().getCodeSource().getLocation();
		System.out.println(path1);
		System.out.println(path2);
		System.out.println(new File(location.toURI()).getAbsolutePath());
		

		File file = new File(path);
		ITesseract instance = new Tesseract();

		// 设置训练库的位置
		// 语言库位置（修改为跟自己语言库文件夹的路径）
		instance.setDatapath("./tessdata");
		// chi_sim ：简体中文， eng 根据需求选择语言库
		instance.setLanguage("eng");

		String result = null;
		try {
			long startTime = System.currentTimeMillis();

			BufferedImage image = ImageIO.read(file);
			result = instance.doOCR(image);
			
			long endTime = System.currentTimeMillis();
			System.out.println("Time is：" + (endTime - startTime) + " 毫秒");
		} catch (TesseractException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		System.out.println("result: ");
		System.out.println(result.trim());
		System.out.println("完毕");

	}
}
